#!/usr/bin/env bash

REGISTRY="docker-registry.easemob.com/kefu/rtc-xswitch"
#VERSION="2.0.3"
VERSION="2.0.5"

MEDIA_IP="123.57.172.41"
SIGNAL_IP="10.251.192.40"
PROXY_API="$SIGNAL_IP:2020"
PROXY_BACKEND="$SIGNAL_IP:2021"
XSW_COUNT=2
XSW_HTTP_PORT_START=3018
XSW_UDP_PORT_START=30000
XSW_UDP_PORT_COUNT=1000
XSW_HOME="/data/apps/rtc-xswitch"
RECORDER_HOME="/data/apps/recorder"

#--log-driver json-file \
#--log-opt max-size=10m \
#--log-opt max-file=3 \
function start_proxy() {
    docker run \
        --restart=always \
        --name "xswitch-proxy" \
        -d \
        --net=host \
        -v /etc/localtime:/etc/localtime:ro \
        -v /data/apps/recorder/job/:/data/apps/recorder/job/ \
        $REGISTRY:$VERSION \
        proxy -api $PROXY_API -backend $PROXY_BACKEND -log-level=*=DEBUG -job-dir=/data/apps/recorder/job/ -log-dir=/data/logs
}

# to limit cpu, add following option
#--cpuset-cpus=$i \
function start_xswitch() {
    for i in $(seq 1 $XSW_COUNT); do
        http_port=$(expr $XSW_HTTP_PORT_START + $i - 1)
        min_port=$(expr $XSW_UDP_PORT_START + \( $i - 1 \) \* $XSW_UDP_PORT_COUNT)
        max_port=$(expr $XSW_UDP_PORT_START + $i \* XSW_UDP_PORT_COUNT - 1)
        cp /app/conf/log4cplus.properties /app/conf/log4cplus.properties.${i}
        sed -i "s/\/data\/rtc-xswitch.log/\/data\/xswitch\/xswitch-$i.log/" /app/conf/log4cplus.properties.${i}
        echo "xswitch $i: http-port=$http_port media-port=$min_port-$max_port"
        docker run --privileged --restart always -d --net=host --name "xswitch-worker-$i" \
            -v /etc/localtime:/etc/localtime:ro \
            -v "$XSW_HOME":/data/ \
            -v "$RECORDER_HOME":/recorder/ \
            "$REGISTRY:$VERSION" \
            rtc-xswitch -c /data/log4cplus.properties.${i} \
            --http-ip $SIGNAL_IP --http-port $http_port  --record-path /recorder/ \
            --min-port $min_port --max-port $max_port --max-video-bitrate 500 \
            --min-video-bitrate 50 \
            --enable-fec \
            --experiment-esmb --lost-0 \
            --translate-fec \
            -m $MEDIA_IP \
            --ipm-ip 127.0.0.1 \
            --proxy-addr $PROXY_BACKEND
    done
}

function stop_proxy() {
    docker stop "xswitch-proxy"
    docker rm "xswitch-proxy"
}

function stop_xswitch() {
    local ctns=$(docker ps -f "name=xswitch-worker" --format "{{.Names}}" -q)
    for c in ${ctns}; do
        docker stop "$c"
        docker rm "$c"
    done
}

function upgrade() {
    local prog=$1
    VERSION=$2
    docker pull "$REGISTRY:$VERSION"
    if [[ $? -ne 0 ]]; then
        echo "unable to pull $REGISTRY:$VERSION"
        exit -1
    fi
    sed -i -re "s/^VERSION=.*$/VERSION=\"$VERSION\"/" "$prog"
}

function logs() {
  local containers=$(docker ps -f "name=xswitch-worker" --format "{{.Names}}" -q)
  echo "please select container"
  select container in ${containers[@]}
  do
    docker logs $container $@
  done
}

case $1 in
    start)
      shift 1
      start_proxy
      start_xswitch
      ;;
    stop)
      shift 1
      stop_xswitch
      stop_proxy
      ;;
    restart)
      shift 1
      stop_xswitch
      stop_proxy
      start_proxy
      start_xswitch
      ;;
    upgrade)
      upgrade $0 $2
      ;;
    logs)
      shift 1
      logs $@
      ;;
esac
